package com.ymttest.business.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ymatou.iapi.sellerproduct.parameter.CatalogLevelBean;
import com.ymatou.iapi.sellerproduct.parameter.es.EsActivity;
import com.ymatou.iapi.sellerproduct.parameter.es.EsCatalog;
import com.ymatou.iapi.sellerproduct.parameter.es.EsLive;
import com.ymatou.iapi.sellerproduct.parameter.es.EsProduct;
import com.ymatou.iapi.sellerproduct.service.ESsearchCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.YmtProductActivityStock;
import com.ymttest.database.model.YmtProductPspInfo;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductPspInfoWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.compare.AssertHelper;

public class EsProductCallService {
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static YmtCatalogsIWapper ymtCatalogsIWapper =  new YmtCatalogsIWapper();
	private static YmtProductPspInfoWapper ymtProductPspInfoWapper = new YmtProductPspInfoWapper();
	private static YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static EsProduct getEsProductFromSql(String productId,List<Integer> liveIds, List<Integer> prodInActivityIds) throws Exception{
		Map<String,Object> productMap = ymtProductsIWapper.selectProductByProductIdforMap(productId).get(0);
		List<Map> catalogsMap = ymtCatalogsIWapper.selectCatalogsByProductIdforMap(productId).stream()
													.filter(t->Integer.parseInt(t.get("iAction").toString())>=0).collect(Collectors.toList());
		
		List<YmtProductPspInfo> pspMapList = ymtProductPspInfoWapper.selectByProductId(productId);
		List<Map> prodTemplatesMapList = ymtProductsIWapper.getProductDeliveryTemplateByProdId(productId); 
		
		
		CatalogLevelBean catalogLevelBean = SellerProductCallService.getProductCategroyByThirdCategoryId(Integer.parseInt(productMap.get("iThirdCategoryId").toString()));

		EsProduct tgtEsProduct = new EsProduct();
		tgtEsProduct.setPid(productId);
		tgtEsProduct.setIpid(Integer.parseInt(productMap.get("ipid").toString()));
		tgtEsProduct.setPname(productMap.get("sProduct").toString());
		tgtEsProduct.setAtime((Date)productMap.get("dAddTime"));
		tgtEsProduct.setStime((Date)productMap.get("validStart"));
		tgtEsProduct.setEtime((Date)productMap.get("validEnd"));
		tgtEsProduct.setLutime((Date)productMap.get("dLastUpdate"));
		tgtEsProduct.setSid(Integer.parseInt(productMap.get("iUserId").toString()));
		tgtEsProduct.setBid(Integer.parseInt(productMap.get("iBrandId").toString()));
		tgtEsProduct.setBname(productMap.get("sBrand").toString());
		tgtEsProduct.setMcateid(catalogLevelBean.getMasterCategoryId());
		tgtEsProduct.setScateid(catalogLevelBean.getSecondCategoryId());
		tgtEsProduct.setTcateid(catalogLevelBean.getThirdCategoryId());
		tgtEsProduct.setCatastatus(Integer.parseInt(productMap.get("iCatalogStatus").toString()));
		tgtEsProduct.setEdtype(Integer.parseInt(productMap.get("iMultiLogistics").toString()));
		tgtEsProduct.setCatatype(Integer.parseInt(productMap.get("iCatalogType").toString()));
		tgtEsProduct.setIstop(Boolean.parseBoolean(productMap.get("isTop").toString()));
		tgtEsProduct.setIsnew(Boolean.parseBoolean(productMap.get("isNew").toString()));
		tgtEsProduct.setNstime((Date)productMap.get("dListingTime"));
		tgtEsProduct.setNetime((Date)productMap.get("dNewEndtime"));
		tgtEsProduct.setIspackage(catalogsMap.stream().map(x->Boolean.parseBoolean(x.get("IsPackage").toString())).anyMatch(y->y));
		tgtEsProduct.setPresale(catalogsMap.stream().map(x->Boolean.parseBoolean(x.get("IsPreSale").toString())).anyMatch(y->y));
		tgtEsProduct.setStock(catalogsMap.stream().map(x->Integer.parseInt(x.get("iNum").toString()))
												  .collect(Collectors.summingInt(Integer::intValue)));
		
		//psp
		if (pspMapList.size()>0){
			tgtEsProduct.setPspstatus(pspMapList.get(0).getPspStatus());
		}else{
			tgtEsProduct.setPspstatus(0);
		}
		
		//free shipping
		tgtEsProduct.setFreeshipping(prodTemplatesMapList.stream().filter(x->Integer.parseInt(x.get("Status").toString())==1).collect(Collectors.toList()).size()<=0);
		
		//catalogs
		List<EsCatalog> esCatalogs = new ArrayList<>();
		for(Map catalogMap : catalogsMap){
			EsCatalog esCatalog = new EsCatalog();
			esCatalog.setNewprice(Double.parseDouble(catalogMap.get("fNewCustomerPrice").toString()));
			esCatalog.setVipprice(Double.parseDouble(catalogMap.get("fVipPrice").toString()));
			esCatalog.setPrice(Double.parseDouble(catalogMap.get("fQuotePrice").toString()));
			esCatalogs.add(esCatalog);
		}
		tgtEsProduct.setCatalogs(esCatalogs);
		
		//lives
		if (liveIds.size()>0){
			List<EsLive> liveProds = new ArrayList<>();
			for(int liveId : liveIds){
				Map<String,Object> liveProdMap = ymtProductsInLiveWapper.selectInLiveByProdIdAndLiveId(productId, liveId).get(0);
				
				EsLive esLive = new EsLive();
				esLive.setLid(Integer.parseInt(liveProdMap.get("iActivityId").toString()));
				esLive.setStime((Date)liveProdMap.get("dStartTime"));
				esLive.setEtime((Date)liveProdMap.get("dEndTime"));
				esLive.setSstatus(Integer.parseInt(liveProdMap.get("iSellStatus").toString()));
				liveProds.add(esLive);
			}
			tgtEsProduct.setLives(liveProds);
		}
		
		//activity
		if (prodInActivityIds.size()>0){
			List<EsActivity> ActivityProds = new ArrayList<>();
			for(int prodInactivityId : prodInActivityIds){
				List<YmtProductActivityStock> ymtProductActivityStockList = ymtProductActivityStockWapper.selectInActivityId(prodInactivityId);
				EsActivity esActivity = new EsActivity();
				esActivity.setAid(ymtProductActivityStockList.get(0).getIactivityid());
				esActivity.setStime(ymtProductActivityStockList.get(0).getDbegintime());
				esActivity.setEtime(ymtProductActivityStockList.get(0).getDendtime());
				esActivity.setStock(ymtProductActivityStockList.stream().map(x->x.getIstocknum()).collect(Collectors.summingInt(Integer::intValue)));
				ActivityProds.add(esActivity);
			}
			
			tgtEsProduct.setActivities(ActivityProds);
		}
		
		
		
		
		
		return tgtEsProduct;
	}
	
	
	public static void checkEsProduct(EsProduct tgtEsProduct, EsProduct actEsProduct){
		Logger.comment("Es 数据检查 商品Id:"+ tgtEsProduct.getPid());
		AssertHelper.assertResultEqual(tgtEsProduct, actEsProduct, "ES数据检查");
		
		//catalog 节点单独来检查
		List<EsCatalog> tgtCatalogs = tgtEsProduct.getCatalogs();
		List<EsCatalog> actCatalogs = actEsProduct.getCatalogs();
		
		if (tgtCatalogs.size()==actCatalogs.size()){
			for(int i=0;i<tgtCatalogs.size();i++){
				AssertHelper.assertResultEqual(tgtCatalogs.get(i), actCatalogs.get(i), "ES数据检查");
			}
			
		}else{
			Logger.verifyEquals(true, false, "Es catalogs 节点个数不一致");
		}
		
				
	}
	
	public static void checkEsProduct(String productId) throws Exception{
		if (YmtMongoDriverFactroy.isEsCheck()){
			List<Integer> lives = LiveCallService.getLivesWithProduct(productId);
			List<Integer> prodInActivityIds = LiveCallService.getProdInActIdsWithProduct(productId);
			EsProduct tgt = EsProductCallService.getEsProductFromSql(productId,lives,prodInActivityIds);
			EsProduct act = new ESsearchCall().callandGetBean(productId);
			EsProductCallService.checkEsProduct(tgt,act);
		}
	}
	
	
	
	
	public static void main(String[] args) throws Exception {
		
		EsProduct a = getEsProductFromSql("7797c1b8-a305-4023-876f-694f59bcae1d",Arrays.asList(160883),Arrays.asList(291721));
		EsProduct b = new ESsearchCall().callandGetBean("7797c1b8-a305-4023-876f-694f59bcae1d");
		checkEsProduct(a,b);
	}
	
	
	
}
